package ibms_rcp.pay.parts;

import ibms_rcp.common.ChooseCompanyDialog;
import ibms_rcp.dao.EstateManager;
import ibms_rcp.dao.PayManager;
import ibms_rcp.model.Building;
import ibms_rcp.model.Company;
import ibms_rcp.model.Floor;
import ibms_rcp.model.Room;
import ibms_rcp.model.SeqGenereter;
import ibms_rcp.tableViewer.TableContentProvider;
import ibms_rcp.tableViewer.TableLabelProvider;
import ibms_rcp.util.SqlUtil;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.Window;
import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;
import org.postgresql.translation.messages_cs;

/**
 * 消费记录界面
 * 
 * @author diamond-wang
 * 
 */
public class QueryPart {

	/*公司id*/
	private Text idText;
	/*公司名称*/
	private Text nameText;
	// 选择公司
	private Button chooseCompanyButton;
	// 结算项目
	private Combo itemCombo;
	// 楼栋
	private ComboViewer buildingComboViewer;
	private Combo buildingCombo;
	// 楼层
	private ComboViewer floorComboViewer;
	private Combo floorCombo;
	// 房间
	private ComboViewer roomComboViewer;
	private Combo roomCombo;
	/* 起始时间 */
	private CalendarCombo dateFromCombo;
	/* 截止时间 */
	private CalendarCombo dateToCombo;
	/* 查询按钮 */
	private Button queryButton;

	private TableViewer tableViewer;
	private Table table;

	@Inject
	public QueryPart(Composite parent) {
	}

	@PostConstruct
	public void postConstruct(final Composite parent) {
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		parent.setLayout(layout);

		Label title = new Label(parent, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setText("消费记录");

		Composite queryComposite = new Composite(parent, SWT.BORDER);
		queryComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		queryComposite.setLayout(new GridLayout(6, false));

		// 公司名称
		Label nameLabel = new Label(queryComposite, SWT.RIGHT);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		nameLabel.setText("公司名称：");
		nameText = new Text(queryComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2,
				1));
		nameText.setEditable(false);
		
		// 选择公司
		chooseCompanyButton = new Button(queryComposite, SWT.NONE);
		chooseCompanyButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				true, false, 1, 1));
		chooseCompanyButton.setText("选择公司");
		//点击弹出选择公司窗口
		chooseCompanyButton.addMouseListener(new MouseAdapter(){
			public void mouseUp(MouseEvent e) {
				ChooseCompanyDialog companyDialog = new ChooseCompanyDialog(parent.getShell());
				companyDialog.create();
				if (companyDialog.open() == Window.OK) {
					Company company = companyDialog.getCompany();
					if(company!=null){
						idText.setText(String.valueOf(company.getId()));
						nameText.setText(company.getName());
					}
				}
			}
		});
		
		// 结算项目
		Label itemLabel = new Label(queryComposite, SWT.RIGHT);
		itemLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		itemLabel.setText("扣费项目：");
		itemCombo = new Combo(queryComposite, SWT.NONE);
		itemCombo.setItems(new String[] { "","物业费", "电费", "照明电费", "动力电费" });
		itemCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		// 楼栋
		Label buildingLabel = new Label(queryComposite, SWT.RIGHT);
		buildingLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		buildingLabel.setText("楼栋：");
		buildingComboViewer = new ComboViewer(queryComposite, SWT.READ_ONLY);
		buildingCombo = buildingComboViewer.getCombo();
		buildingCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		buildingComboViewer.setContentProvider(ArrayContentProvider
				.getInstance());
		buildingComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Building) {
					Building building = (Building) element;
					return building.getEms_name();
				}
				return super.getText(element);
			}
		});
		// 模拟数据
		// Building[] buildings = new Building[] { new Building(1, "楼1", "1"),
		// new Building(2, "楼2", "2"), new Building(3, "楼3", "3") };
		// 获取楼数据
		List<Building> buildings = EstateManager.getBuildings();
		buildingComboViewer.setInput(buildings);

		buildingComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						long buildingId = ((Building) selection
								.getFirstElement()).getId();
						// 获取楼层数据
						List<Floor> floors = EstateManager
								.getFloorsByBuilding(buildingId);
						floorComboViewer.setInput(floors);
						roomComboViewer.setInput(null);
					}
				});

		// 楼层
		Label floorLabel = new Label(queryComposite, SWT.RIGHT);
		floorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		floorLabel.setText("楼层：");
		floorComboViewer = new ComboViewer(queryComposite, SWT.READ_ONLY);
		floorCombo = floorComboViewer.getCombo();
		floorCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		floorComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		floorComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Floor) {
					Floor floor = (Floor) element;
					return floor.getEms_name();
				}
				return super.getText(element);
			}
		});

		floorComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						long floorId = ((Floor) selection.getFirstElement())
								.getId();
						// 获取楼层数据
						List<Room> rooms = EstateManager
								.getRoomsByFloor(floorId);
						roomComboViewer.setInput(rooms);
					}
				});

		// 房间号
		Label roomLabel = new Label(queryComposite, SWT.RIGHT);
		roomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		roomLabel.setText("房间：");
		roomComboViewer = new ComboViewer(queryComposite, SWT.READ_ONLY);
		roomCombo = roomComboViewer.getCombo();
		roomCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		roomComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		roomComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				if (element instanceof Room) {
					Room room = (Room) element;
					return room.getEms_name();
				}
				return super.getText(element);
			}
		});
		roomComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
					}
				});

		// 起始日期
		Label dateFromLabel = new Label(queryComposite, SWT.RIGHT);
		dateFromLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		dateFromLabel.setText("从");
		dateFromCombo = new CalendarCombo(queryComposite, SWT.BORDER);
		dateFromCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		// 截止日期
		Label dateToLabel = new Label(queryComposite, SWT.RIGHT);
		dateToLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		dateToLabel.setText("到");
		dateToCombo = new CalendarCombo(queryComposite, SWT.BORDER);
		dateToCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

//		new Label(queryComposite, SWT.NONE);
		// 查询按钮
		queryButton = new Button(queryComposite, SWT.NONE);
		queryButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				IStructuredSelection selection = (IStructuredSelection) roomComboViewer
						.getSelection();
				Room room = (Room) selection.getFirstElement();
				Long room_moi_id = null;
				if (room != null) {
					room_moi_id = room.getId();
				}
				Long company_moi_id = null;
				
				String bill_type = itemCombo.getText();
				String begin = dateFromCombo.getDateAsString();
				String end = dateToCombo.getDateAsString();
				if(bill_type.length() == 0){
					bill_type = null;
				}
				if (begin.length() == 0) {
					begin = null;
				}
				if (end.length() == 0) {
					end = null;
				}
				
				System.out.println("bill_type:"+bill_type);
				System.out.println("begin:"+begin);
				System.out.println("end:"+end);

				int state = Verify(room_moi_id, company_moi_id);

				SeqGenereter.clearseq();
				switch (state) {
				case 0:
					MessageDialog.openWarning(parent.getShell(), "警告",
							"您输入的查询条件不合法");
					break;
				case 1:
					tableViewer.setInput(PayManager
							.getPayLogsbyCompany_id(company_moi_id,bill_type,begin,end));
					break;
				case 2:
					tableViewer.setInput(PayManager
							.getPayLogsbyRoom_id(room_moi_id,bill_type,begin,end));
					break;
				case 3:
					tableViewer.setInput(PayManager
							.getPayLogsbyCompany_Room(company_moi_id,room_moi_id,bill_type,begin,end));
					break;
				default:
					break;
				}
			}
		});
		queryButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		queryButton.setText("查询");

		//公司moi id
		idText = new Text(queryComposite, SWT.BORDER);
		idText.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		idText.setEditable(false);
		idText.setVisible(false);

		tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		TableViewerColumn seqViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn seqTableColumn = seqViewerColumn.getColumn();
		seqTableColumn.setResizable(false);
		seqTableColumn.setAlignment(SWT.CENTER);
		seqTableColumn.setWidth(40);
		seqTableColumn.setText("序号");

		TableViewerColumn nameViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn nameTableColumn = nameViewerColumn.getColumn();
		nameTableColumn.setAlignment(SWT.CENTER);
		nameTableColumn.setWidth(200);
		nameTableColumn.setText("公司名称");

		TableViewerColumn buildingViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn buildingTableColumn = buildingViewerColumn.getColumn();
		buildingTableColumn.setResizable(false);
		buildingTableColumn.setAlignment(SWT.CENTER);
		buildingTableColumn.setWidth(100);
		buildingTableColumn.setText("楼栋");

		TableViewerColumn floorViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn floorTableColumn = floorViewerColumn.getColumn();
		floorTableColumn.setResizable(false);
		floorTableColumn.setAlignment(SWT.CENTER);
		floorTableColumn.setWidth(100);
		floorTableColumn.setText("楼层");

		TableViewerColumn roomViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn roomTableColumn = roomViewerColumn.getColumn();
		roomTableColumn.setResizable(false);
		roomTableColumn.setAlignment(SWT.CENTER);
		roomTableColumn.setWidth(100);
		roomTableColumn.setText("房间");

		TableViewerColumn timeViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn timeTableColumn = timeViewerColumn.getColumn();
		timeTableColumn.setResizable(false);
		timeTableColumn.setAlignment(SWT.CENTER);
		timeTableColumn.setWidth(180);
		timeTableColumn.setText("扣费时间");

		TableViewerColumn itemViewerColumn = new TableViewerColumn(tableViewer,
				SWT.NONE);
		TableColumn itemTableColumn = itemViewerColumn.getColumn();
		itemTableColumn.setResizable(false);
		itemTableColumn.setAlignment(SWT.CENTER);
		itemTableColumn.setWidth(100);
		itemTableColumn.setText("扣费项目");

		TableViewerColumn oldvalueViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn oldvalueColumn = oldvalueViewerColumn.getColumn();
		oldvalueColumn.setResizable(false);
		oldvalueColumn.setAlignment(SWT.CENTER);
		oldvalueColumn.setWidth(100);
		oldvalueColumn.setText("上次数目");

		TableViewerColumn newvalueViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn newvalueColumn = newvalueViewerColumn.getColumn();
		newvalueColumn.setResizable(false);
		newvalueColumn.setAlignment(SWT.CENTER);
		newvalueColumn.setWidth(100);
		newvalueColumn.setText("目前数目");

		TableViewerColumn costvalueViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn costvalueColumn = costvalueViewerColumn.getColumn();
		costvalueColumn.setResizable(false);
		costvalueColumn.setAlignment(SWT.CENTER);
		costvalueColumn.setWidth(100);
		costvalueColumn.setText("消耗量");

		TableViewerColumn moneyViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn moneyTableColumn = moneyViewerColumn.getColumn();
		moneyTableColumn.setResizable(false);
		moneyTableColumn.setAlignment(SWT.CENTER);
		moneyTableColumn.setWidth(100);
		moneyTableColumn.setText("金额(元)");

		TableViewerColumn balanceViewerColumn = new TableViewerColumn(
				tableViewer, SWT.NONE);
		TableColumn balanceTableColumn = balanceViewerColumn.getColumn();
		balanceTableColumn.setResizable(false);
		balanceTableColumn.setAlignment(SWT.CENTER);
		balanceTableColumn.setWidth(100);
		balanceTableColumn.setText("余额(元)");

		tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
	}

	// 验证查询消费记录的表单项，返回值的为int，几个值含义如下
	// 0:表单填写不合法。 1：填写了公司，没写房间 。2：填写房间没写公司。3：都填写了
	public int Verify(Long room_moi_id, Long company_moi_id) {
		// TODO Auto-generated method stub
		if (room_moi_id == null && company_moi_id == null) {
			return 0;
		} else if (room_moi_id == null) {
			return 1;
		} else if (company_moi_id == null) {
			return 2;
		} else {
			return 3;
		}
	}

	@PreDestroy
	public void preDestroy() {
		// TODO Your code here
	}
}